Imports System
Imports System.Windows.Forms

Namespace SerialPortProfileApplication
	Public Partial Class Form1
        Inherits Form

        Private WithEvents sp As IO.Ports.SerialPort = Nothing
		Private displayString As String = ""
        Private [error] As String = ""

		Public Sub New()
			InitializeComponent()
			btnSend.Enabled = False
		End Sub

        Private Sub btnSet_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSet.Click
            Try

                If String.IsNullOrEmpty(cmbPortName.Text) Then
                    Microsoft.VisualBasic.MsgBox("Please enter a valid Port name for e.g. COM1", Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or Microsoft.VisualBasic.MsgBoxStyle.Critical, "Legacy Application")
                    cmbPortName.Focus()
                Else
                    sp = New IO.Ports.SerialPort(cmbPortName.Text)
                    sp.Open()
                    btnSet.Enabled = False
                    cmbPortName.Enabled = False
                End If
            Catch ex As Exception
                Dim errMessage As String = ""

                If ex.InnerException IsNot Nothing Then
                    errMessage = ex.Message + ex.InnerException.Message
                Else
                    errMessage = ex.Message
                End If

                Microsoft.VisualBasic.MsgBox(errMessage, Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or Microsoft.VisualBasic.MsgBoxStyle.Critical, "Legacy Application")
                btnSet.Enabled = True
                cmbPortName.Text = String.Empty
            End Try
        End Sub

        Private Sub sp_ErrorReceived(ByVal sender As Object, ByVal e As IO.Ports.SerialErrorReceivedEventArgs) Handles sp.ErrorReceived
            [error] = sp.ReadExisting()
            Me.Invoke(New EventHandler(AddressOf SetErrorText))
        End Sub

        Private Sub sp_DataReceived(ByVal sender As Object, ByVal e As IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
            displayString = sp.ReadExisting()
            Me.Invoke(New EventHandler(AddressOf DisplayReceiveData))
        End Sub

        Private Sub btnExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnExit.Click
            If sp IsNot Nothing Then
                If sp.IsOpen Then
                    sp.Close()
                End If
            End If
            Dispose()
        End Sub

        Private Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
            If sp Is Nothing Then
                Microsoft.VisualBasic.MsgBox("Please connect to the serial port before sending data.", Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or Microsoft.VisualBasic.MsgBoxStyle.Critical, "Legacy Application")
                Return
            End If

            Try
                If txtBoxLegacyApp.Text <> "" Then
                    sp.WriteLine(txtBoxLegacyApp.Text)
                    txtBoxLegacyAppSent.Text += "> " + txtBoxLegacyApp.Text.Trim() + Environment.NewLine
                    txtBoxLegacyApp.Text = ""
                End If
            Catch ex As Exception
                Microsoft.VisualBasic.MsgBox("An error occurred while sending the data.", Microsoft.VisualBasic.MsgBoxStyle.OkOnly Or Microsoft.VisualBasic.MsgBoxStyle.Critical, "Legacy Application")
            End Try
        End Sub

		Public Sub DisplayReceiveData(ByVal sender As Object, ByVal e As EventArgs)
			txtBoxRemoteServ.Text += "> " + displayString.Trim() + Environment.NewLine

        End Sub

		Public Sub SetErrorText(ByVal sender As Object, ByVal e As EventArgs)
			If [error] <> "" Then
				Throw New Exception([error])
			End If

        End Sub

        Private Sub txtBoxLegacyApp_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtBoxLegacyApp.TextChanged
            If String.IsNullOrEmpty(txtBoxLegacyApp.Text) Then
                btnSend.Enabled = False
            Else
                btnSend.Enabled = True
            End If

        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Dim portNames() As String
            portNames = IO.Ports.SerialPort.GetPortNames()
            For Each port As String In portNames
                If Not cmbPortName.Items.Contains(port) Then
                    cmbPortName.Items.Add(port)
                End If
            Next
        End Sub
    End Class
End Namespace
